package com.anta.it.hr.devtools.reactive.impl;

import com.anta.it.hr.devtools.core.request.impl.AbstractRequestInetAddress;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.server.reactive.ServerHttpRequest;

import java.net.InetSocketAddress;

/**
 * 响应式容器的请求，如spring网关的webFlux
 * @author wangxiaoxian
 * @since 2021/8/4
 */
@Slf4j
public class ReactiveRequestInetAddress extends AbstractRequestInetAddress {
    @Override
    public String getIpFromHeader(String key, Object loadBalanceKey) {
        return ((ServerHttpRequest)loadBalanceKey).getHeaders().getFirst(key);
    }

    @Override
    public String getRemoteIp(Object loadBalanceKey) {
        InetSocketAddress remoteAddress = ((ServerHttpRequest)loadBalanceKey).getRemoteAddress();
        if (remoteAddress != null) {
            return remoteAddress.getHostName();
        }
        return null;
    }
}
